<?php
namespace addon\mall\controller;
use app\common\controller\AddonAdminBase;
use tpfcore\Core;
use think\Cookie;
// +----------------------------------------------------------------------
// | Author: yaoyihong <510974211@qq.com>
// +----------------------------------------------------------------------
/**
 * ============================================================================
 * 版权所有 2017-2077 tpframe工作室，并保留所有权利。
 * @link http://www.tpframe.com/
 * @copyright Copyright (c) 2017 TPFrame Software LLC
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！未经本公司授权您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布
 * ============================================================================
 */
class Order extends AddonAdminBase
{
    public function index(){
      $data=$search = $this->param;
      $this->assign('search',$search);
      $where=[];
      $begin=$end='';
      if(isset($data['order_status']) && is_numeric($data['order_status'])){
        $where['order_status']=$data['order_status'];
      }
      if(isset($data['pay_status']) && is_numeric($data['pay_status'])){
        $where['pay_status']=$data['pay_status'];
      }
      if(isset($data['shipping_status']) && is_numeric($data['shipping_status'])){
        $where['shipping_status']=$data['shipping_status'];
      }
      if(isset($data['pay_way']) && !empty($data['pay_way'])){
        $where['pay_handle']=$data['pay_way'];
      }
      !empty($data['start_time']) ? $begin=strtotime($data['start_time']) : '';
      empty($data['end_time']) ? $end=time() : $end=strtotime($data['end_time']);
      if($begin && $end){
        $where['add_time'] = ['between',"$begin,$end"];
      }
    	$this->addonTemplate('index',[
        "list"=>Core::loadAddonModel($this->name)->getOrder([
            "where"=>$where,
        		"order"=>"id asc",
        		'paginate'=>['rows' => 10,"config"=>["query"=>$this->param]],
        ])
    	]);
    }
    // 后面版本完善
    public function info(){
      $list = Core::loadAddonModel($this->name)->getOrder([
              "field"=>"__ORDER__.*,__USER__.username",
              "where"=>["__ORDER__.id"=>$this->param['id']],
              "join"=>["join"=>"__USER__","condition"=>"__USER__.id=__ORDER__.user_id","type"=>"left"]
          ]);

      $list[0]['goods']=Core::loadAddonModel("OrderGoods")->getOrderGoods(["where"=>["order_id"=>$list[0]['id']]]);
        
      $listOrderAction = Core::loadAddonModel("OrderAction")->getOrderAction([
        "field"=>"__ORDER_ACTION__.*,__USER__.username,__USER__.type",
        "where"=>["order_id"=>$list[0]['id']],
        "order"=>"datetime desc",
        "join"=>["join"=>"__USER__","condition"=>"__USER__.id=__ORDER_ACTION__.user_id"]
      ]);

    	$this->addonTemplate('info',[
          "list"=>$list,
          "listOrderAction"=>$listOrderAction
      	]);
    }

    public function ship_list(){
      $this->addonTemplate('ship_list',[
          "list"=>Core::loadAddonModel($this->name)->getOrder([
              "where"=>["shipping_status"=>2,"order_status"=>1,"pay_status"=>1],
              "order"=>"id asc",
              'paginate'=>['rows' => 10],
          ])
        ]);
    }
}
